﻿using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using WaterCloud.Code;
using SqlSugar;
using WaterCloud.DataBase;
using WaterCloud.Domain.SocialSecurityManagement;
using WaterCloud.Domain.AttendanceManagement;
using WaterCloud.Domain.SystemOrganize;
using WaterCloud.Domain.ContractManagement;

namespace WaterCloud.Service.SocialSecurityManagement
{
    /// <summary>
    /// 创 建：超级管理员
    /// 日 期：2025-04-18 14:25
    /// 描 述：已缴纳服务类
    /// </summary>
    public class InsuranceService : BaseService<InsuranceEntity>, IDenpendency
    {
        public InsuranceService(ISqlSugarClient context) : base(context)
        {
        }
        #region 获取数据
        public async Task<List<InsuranceEntity>> GetList(string keyword = "")
        {
            var data = repository.IQueryable();
            if (!string.IsNullOrEmpty(keyword))
            {
                data = data.Where(a => a.F_UserNum.Contains(keyword)
                || a.F_RealName.Contains(keyword));
            }
            return await data.Where(a => a.F_DeleteMark == false).OrderBy(a => a.F_Id , OrderByType.Desc).ToListAsync();
        }

        public async Task<List<InsuranceEntity>> GetLookList(string keyword = "")
        {
            var query = repository.IQueryable().Where(a => a.F_DeleteMark == false);
            if (!string.IsNullOrEmpty(keyword))
            {
                //此处需修改
                query = query.Where(a => a.F_UserNum.Contains(keyword)
                || a.F_RealName.Contains(keyword));
            }
             //权限过滤
             query = GetDataPrivilege("a", "", query);
             return await query.OrderBy(a => a.F_Id , OrderByType.Desc).ToListAsync();
        }

        public async Task<List<InsuranceEntity>> GetLookList(SoulPage<InsuranceEntity> pagination,string keyword = "",string id="")
        {
            var query = repository.Db.Queryable<InsuranceEntity,UserEntity,OrganizeEntity>((a, b,c) => new JoinQueryInfos(
            JoinType.Left, a.F_UserId == b.F_Id,
              JoinType.Left, b.F_OrganizeId == c.F_Id
            )).Where((a,b,c)=>(b.F_WorkState==0||b.F_WorkState==1)&&a.F_DeleteMark==false)
            .Select((a, b,c) => new InsuranceEntity
            {
                F_Id = a.F_Id.SelectAll(),
            
                F_OrganizeName=c.F_FullName,

            }).MergeTable();
          
            if (!string.IsNullOrEmpty(keyword))
            {
                query = query.Where(a => a.F_UserNum.Contains(keyword)
                || a.F_RealName.Contains(keyword));
            }
            if(!string.IsNullOrEmpty(id))
            {
                query= query.Where(a=>a.F_Id==id);
            }
           

            //权限过滤
            query = GetDataPrivilege("a","",query);
            return  await query.ToPageListAsync(pagination);
        }
        public async Task<List<InsuranceEntity>> GetGridJson2(SoulPage<InsuranceEntity> pagination, string keyword = "")
        {
            var query = repository.Db.Queryable<InsuranceEntity, UserEntity, OrganizeEntity>((a, b,c) => new JoinQueryInfos(
                JoinType.Left, a.F_UserId == b.F_Id,
                   JoinType.Left, b.F_OrganizeId == c.F_Id
                )).Where((a, b,c) => a.F_DeleteMark == false && b.F_WorkState == 2)
                .Select((a, b,c) => new InsuranceEntity
                {
                    F_Id = a.F_Id.SelectAll(),
              
                    F_OrganizeName = c.F_FullName,
                }).MergeTable();

            if (!string.IsNullOrEmpty(keyword))
            {
                //此处需修改

                query = query.Where(a => a.F_UserNum.Contains(keyword)
                || a.F_RealName.Contains(keyword));
            }
            //权限过滤
            query = GetDataPrivilege("a", "", query);
            return await query.ToPageListAsync(pagination);

        }
        public async Task<List<InsuranceEntity>> GetGridJson3(SoulPage<InsuranceEntity> pagination, string keyword = "")
        {
            var query = repository.Db.Queryable<InsuranceEntity, UserEntity, OrganizeEntity>((a, b, c) => new JoinQueryInfos(
                JoinType.Left, a.F_UserId == b.F_Id,
                   JoinType.Left, b.F_OrganizeId == c.F_Id
                )).Where((a, b, c) => a.F_DeleteMark == true)
                .Select((a, b, c) => new InsuranceEntity
                {
                    F_Id = a.F_Id.SelectAll(),
              
                    F_OrganizeName = c.F_FullName,
                }).MergeTable();



            if (!string.IsNullOrEmpty(keyword))
            {
                //此处需修改

                query = query.Where(a => a.F_UserNum.Contains(keyword)
                || a.F_RealName.Contains(keyword));
            }
            //权限过滤
            query = GetDataPrivilege("a", "", query);
            return await query.ToPageListAsync(pagination);

        }
        public async Task<InsuranceEntity> GetForm(string keyValue)
        {
            var data = await repository.FindEntity(keyValue);
            return data;
        }
        #endregion

        public async Task<InsuranceEntity> GetLookForm(string keyValue)
        {

            var data = await repository.FindEntity(keyValue);
            return GetFieldsFilterData(data);


        }

        #region 提交数据
        public async Task SubmitForm(InsuranceEntity entity, string keyValue)
        {
            if(string.IsNullOrEmpty(keyValue))
            {
                    //初始值添加
                entity.F_DeleteMark = false;
                entity.Create();
                await repository.Insert(entity);
            }
            else
            {
                    //此处需修改
                entity.Modify(keyValue); 
                await repository.Update(entity);
            }
        }

        public async Task DeleteForm(string keyValue)
        {
            //var ids = keyValue.Split(',');
            //await repository.Delete(a => ids.Contains(a.F_Id.ToString()));
            var ids = keyValue.Split(',');
            await repository.Update(a => ids.Contains(a.F_Id), a => new InsuranceEntity
            {
                F_EnabledMark = false,
                F_DeleteMark = true,
                F_DeleteTime = DateTime.Now,
                F_DeleteUserId = currentuser.UserId,
            });
        }
        #endregion

    }
}
